﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
//using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;

public partial class BackGround_CategoryEdit : System.Web.UI.Page
{
    Operation operation = new Operation(); //业务类对象
    string tid = "";
    string pid = "";
    int op = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin"] != null)
        {
            string opt = Request.QueryString["op"].ToString();
            tid = Request.QueryString["typeid"];
            if (opt.CompareTo("0") == 0)//根据获取的字符串判断是修改还是增加操作
            {
                if(tid.Equals("0"))
                {
                    WebMessageBox.Show("顶级类别不能修改！");
                    return;
                }
                op = 0;//修改类别
                try
                {
                    pid = operation.SelectCategoryByID(tid).Tables[0].Rows[0][2].ToString();
                }
                catch (Exception ex) {  }
            }
            else
            {
                op = 1;
            }
            if (!IsPostBack)
            {
                ListItem item = new ListItem();
                item.Value = "0";
                item.Text = "顶级分类";
                dlCategory.Items.Add(item);//构建分类下拉框
                BuildDownList("0", "", "---");
                if (op == 0)
                {//修改视图
                    dlCategory.Text = pid;
                    dlCategory.Enabled = false;
                    btSave.Text = "修改";
                    try
                    {
                        DataSet ds = operation.SelectCategoryByID(tid);
                        typeName.Text = ds.Tables[0].Rows[0][1].ToString();
                        if (ds.Tables[0].Rows[0][3].ToString().CompareTo("False") == 0)
                            rdoFalse.Checked = true;
                        else
                            rdoTrue.Checked = true;
                    }
                    catch (Exception ex) { }
                }
                else
                {//增加视图
                    dlCategory.Text = tid;
                    btSave.Text = "添加";
                }
            }
        }
        else { return; }
    }
    public void BuildDownList(string pid, string itH, string dx)//itH为表项前度，dx为递增前度
    {
        DataSet ds = operation.SelectCategoryByPID(pid);//一级类别
        int count = ds.Tables[0].Rows.Count;
        string t = itH + dx;//下级前度
        for (int i = 0; i < count; i++)
        {
            ListItem item = new ListItem();
            item.Value = ds.Tables[0].Rows[i][0].ToString();
            item.Text = itH + ds.Tables[0].Rows[i][1].ToString();
            dlCategory.Items.Add(item);
            BuildDownList(item.Value, t, dx);
        }
    }
    protected void btSave_Click(object sender, EventArgs e)
    {
        string type = typeName.Text.Trim();
        string id = dlCategory.Text;
        bool enable = rdoTrue.Checked;
        if (op == 0)
        {//修改
            string msg = "";
            if(id.Equals("0")||operation.SelectCategoryByID(id).Tables[0].Rows[0][3].ToString().Equals("True"))
            {
            operation.UpdateTypeName(tid, type, enable);
            msg = "修改成功！";
            }else{
                msg="修改失败,请将父级设为显示!";
            }
            String Src = "CategoryManager.aspx";
            Response.Write("<script language='javascript' type='text/javascript'>alert('" +msg+
                "');document.location.href='" + Src + "'</script>");
        }
        else
        {//添加
            if (operation.SelectCategoryByName(type, dlCategory.Text).Tables[0].Rows.Count <= 0)
            {
                operation.InsertCategory(type, Convert.ToInt32(dlCategory.Text), enable);
                //WebMessageBox.Show("添加成功！", "CategoryEdit.aspx?op=1&typeid=" + Convert.ToInt32(dlCategory.Text));
                
                //WebMessageBox.Show("添加成功！", "CategoryManager.aspx");
                //String Src = "CategoryManager.aspx";
                Response.Write("<script language='javascript' type='text/javascript'>alert('添加成功！"+
                    "');</script>");
                typeName.Text = "";
                dlCategory.Items.Clear();
                ListItem item = new ListItem();
                item.Value = "0";
                item.Text = "顶级分类";
                dlCategory.Items.Add(item);//构建分类下拉框
                BuildDownList("0", "", "---");
            }
            else
            {
                WebMessageBox.Show("添加失败,该父级下已存在同名分类！");
                return;
            }
        }
    }
}
